package com.health.es.config;

import com.health.es.temp.EsCustomImpl;
import com.health.es.temp.EsHelper;
import com.health.es.util.ElasticsearchOperations;
import com.health.es.util.ElasticsearchRestTemplate;
import com.health.es.util.convert.ElasticsearchConverter;
import com.health.es.util.repository.config.EnableElasticsearchRepositories;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;

/**
 * @author qyh
 * ES 默认配置
 */
@Configuration
@Slf4j
@ComponentScan(basePackages = {"com.health.es.util"})
@EnableElasticsearchRepositories(basePackages = {"com.health.es.repository"}, repositoryBaseClass = EsCustomImpl.class)
public class ElasticsearchConfig {

    @Autowired
    private RestTemplateBuilder builder;

    @Bean
    public RestTemplate restTemplate() {
        return builder.build();
    }

    @Bean
    @Primary
    public ElasticsearchRestTemplate elasticsearchRestTemplate(RestHighLevelClient restHighLevelClient) {
        return new ElasticsearchRestTemplate(restHighLevelClient);
    }

    @Bean(name = {"elasticsearchOperations", "elasticsearchTemplate"})
    public ElasticsearchOperations elasticsearchOperations(ElasticsearchConverter elasticsearchConverter,
                                                           RestHighLevelClient elasticsearchClient) {
        ElasticsearchRestTemplate elasticsearchRestTemplate = new ElasticsearchRestTemplate(elasticsearchClient, elasticsearchConverter);
        EsHelper.operations=elasticsearchRestTemplate;
        return elasticsearchRestTemplate;
    }


}
